<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="utf-8" %>
<script runat="server">
	const string filePath = "/public/tinyimg";
	const string validExt = ".jpg,.gif,.png";
	int maxsize = 4096;
	string currentOp = "";
	string currentFile = "";
	
	string _uperror = "";
	public string pubBase = "";
	public string istanzaJS ="";
	public string execJS ="";	
	public string mW="800";
	public string mH ="600";
	
	private string CurrentKey
	{
		get
		{
			String s = (String)ViewState[this.ID + "FileKey"];
			return ((s == null) ? String.Empty : s);
		}

		set
		{
			ViewState[this.ID + "FileKey"] = value;
		}
	}	
		
	
	void Page_Load(object o, EventArgs e) {
		if(!Page.IsPostBack) {
			currentOp = "upd";
			
		} else {
			currentOp = "del";
			UploadMyImage();
		}
	}

	void UploadMyImage() {
		if (fo_up.HasFile)
			{
				//Controllo size:
				string filename = fo_up.FileName;
				string ext = "";
				if (filename.IndexOf('.') == -1) { _uperror = "NO VALID SOURCE FILE"; currentOp="upd";return; }
				ext = filename.Substring(filename.LastIndexOf('.'));
				if(validExt.Trim().Length>0) {
					if (validExt.ToLower().IndexOf(ext.ToLower()) == -1) { _uperror = "NO VALID FILE FORMAT"; currentOp="upd";return; }
				} 
				
				byte[] lettore = fo_up.FileBytes;
				
				if (lettore.Length > (maxsize * 1024)) { _uperror = "FILE TOO LONG"; currentOp="upd";return; }
				
				//se è un img e vogliamo bloccarla al max size previsto dalla configurazione.
				try {
					byte[] lettore2 = ACM.cmsarea08.Function.TextClass.textclass.ImageResize(lettore,mW,mH);
					lettore = lettore2; 
					_uperror="The image is more bigger than the maximum allowed size, it will be resized.";
				} catch (Exception ex) {
					_uperror="Unable to upload image, file format invalid.\\n" + ex.ToString();
					currentOp="upd";
					lettore=new byte[0];
				}
				if(lettore.Length==0) { _uperror = "THE FILE IS AN IMAGE. BUT IT'S TOO BIG."; return; }
				ClearCache();
				ACM.cmsarea08.Function.CacheManager.cachemanager.setCacheFileObject(
                	HttpContext.Current,CurrentKey,filename.Replace("#","") +"#" +Convert.ToBase64String(lettore),180,true);
				execJS = "window.parent.ImageDialog.LoadPreview('"+CurrentKey+"','"+filename+"');";	
				lettore=new byte[0];
			} else {
				currentOp="upd";
			}			
	}

	private void ClearCache()
	{
		if (CurrentKey == "") { CurrentKey = "FILMAN" + System.Guid.NewGuid().ToString(); return; }
		ACM.cmsarea08.Function.CacheManager.cachemanager.RemoveCacheFileObject(HttpContext.Current,CurrentKey);
		CurrentKey =  "FILMAN" + System.Guid.NewGuid().ToString();
		return;
	}
	
	
	void ClearMe(object o, EventArgs e) {
		ClearCache();
		execJS = "window.parent.ImageDialog.ClearPreview();";			
	}

</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Documento senza titolo</title>
    <link href="css/advimage.css" rel="stylesheet" type="text/css" />
	<base target="_self" />
</head>
<body>
<form id="formUpload" runat="server">
<div id="innerloadImg">
<asp:FileUpload id="fo_up" class="btmEdit updbtm" runat="server"></asp:fileupload>
<asp:Button id="save" CssClass="btmEdit" runat="server" Text="salva"></asp:Button>
</div>
<div id="innerdelImg">
<asp:Button id="deltemp" CssClass="btmEdit" runat="server" Text="elimina" OnClick="ClearMe"></asp:Button>
</div>
<script language="javascript">
	function carica() {
		var quale = "";
		if('<%=currentOp%>'=='del')
			quale = 'innerloadImg';
		else
			quale = 'innerdelImg';
		var elem = document.getElementById(quale);
		if(elem) 
			elem.style.display='none';
/*	
		var cont = window.parent.tinyMCEPopup.dom.get(quale);
		if(cont)
			window.parent.tinyMCEPopup.dom.setStyle(quale,'display','none');
*/			
	}

	carica();
	<%= execJS %>
</script>
</form>
</body>
</html>
